package Servlet;

import DAO.DAOFactory;
import DAO.MaintenanceDAO;
import javabean.Maintenance;
import javabean.Technician;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/pendingMaintenance")
public class pendingMaintenance extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        Technician loginTechnician = (Technician) session.getAttribute("loginTechnician");
        Maintenance maintenance = (Maintenance) req.getSession().getAttribute("maintenance");
        MaintenanceDAO maintenanceDAO = (MaintenanceDAO) DAOFactory.getMaintenanceDAOInstance();
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        try {
            List<Maintenance> maintenances = maintenanceDAO.getPendingMaintance(loginTechnician.getTechnicianID());
            req.setAttribute("maintenances", maintenances);
            out.print("<script>alert('操作成功！')</script>");
            req.getRequestDispatcher("pendingMaintenance.jsp").forward(req, resp);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
